home *** CD-ROM | disk | FTP | other *** search
- { getbits.pas -- Retrieve bitmap (if present) from clipboard }
-
- program GetBits;
-
- {$R getbits.res}
-
- uses WinTypes, WinProcs, WObjects;
-
- const
-
- id_Menu = 100; { Menu resource ID }
- cm_Quit = 101; { Exit command ID }
- cm_Paste = 201; { Paste command ID }
- cm_Clear = 202; { Clear command ID }
-
- type
-
- GetBitsApplication = object(TApplication)
- procedure InitMainWindow; virtual;
- end;
-
- PGetBitsWindow = ^GetBitsWindow;
- GetBitsWindow = object(TWindow)
- Bitmap: HBitmap;
- Width, Height: LongInt; { Size of bitmap image }
- HTheMenu: HMenu; { Handle to menu with Paste command }
- constructor Init(AParent: PWindowsObject; ATitle: PChar);
- destructor Done; virtual;
- procedure DestroyBitmap;
- procedure Paint(PaintDC: HDC;
- var PaintInfo: TPaintStruct); virtual;
- procedure WMInitMenuPopup(var Msg: TMessage);
- virtual wm_First + wm_InitMenuPopup;
- procedure CMQuit(var Msg: TMessage);
- virtual cm_First + cm_Quit;
- procedure CMPaste(var Msg: TMessage);
- virtual cm_First + cm_Paste;
- procedure CMClear(var Msg: TMessage);
- virtual cm_First + cm_Clear;
- end;
-
-
- { GetBitsApplication }
-
- {- Initialize GetBitsApplication object's window }
- procedure GetBitsApplication.InitMainWindow;
- begin
- MainWindow := New(PGetBitsWindow, Init(nil, 'Get Bitmap'))
- end;
-
-
- { GetBitsWindow }
-
- {- Construct GetBitsWindow object }
- constructor GetBitsWindow.Init(AParent: PWindowsObject; ATitle: PChar);
- begin
- TWindow.Init(AParent, ATitle);
- Attr.Menu := LoadMenu(HInstance, PChar(id_Menu));
- HTheMenu := GetSubMenu(Attr.Menu, 1); { Handle to "Bitmap" menu }
- Bitmap := 0
- end;
-
- {- Destroy GetBitsWindow }
- destructor GetBitsWindow.Done;
- begin
- DestroyBitmap;
- TWindow.Done
- end;
-
- {- Destroy window's bitmap if present }
- procedure GetBitsWindow.DestroyBitmap;
- begin
- if Bitmap <> 0 then
- begin
- DeleteObject(Bitmap);
- Bitmap := 0
- end
- end;
-
- {- Enable or disable paste and clear commands }
- procedure GetBitsWindow.WMInitMenuPopup(var Msg: TMessage);
- begin
- if Msg.WParam = HTheMenu then
- begin
- EnableMenuItem(HTheMenu, cm_Paste, mf_ByCommand or mf_Grayed);
- if Bitmap <> 0 then
- EnableMenuItem(HTheMenu, cm_Clear, mf_ByCommand or mf_Enabled)
- else
- EnableMenuItem(HTheMenu, cm_Clear, mf_ByCommand or mf_Grayed);
- if OpenClipboard(HWindow) then
- begin
- if IsClipboardFormatAvailable(cf_Bitmap) then
- EnableMenuItem(HTheMenu, cm_Paste, mf_ByCommand or mf_Enabled);
- CloseClipboard
- end
- end
- end;
-
- {- Exit application }
- procedure GetBitsWindow.CMQuit(var Msg: TMessage);
- begin
- CloseWindow
- end;
-
- {- Paste bitmap from clipboard into window }
- procedure GetBitsWindow.CMPaste(var Msg: TMessage);
- var
- HClip: THandle; { Handle to clipboard data }
- HPtr: Pointer; { Pointer to locked data }
- DC, MemDC1, MemDC2: HDC; { Display context handles }
- OldBits1, OldBits2: HBitmap; { Handles to old DC bitmaps }
- BitmapRec: TBitmap; { Bitmap info record }
- begin
- DestroyBitmap;
- if OpenClipboard(HWindow) then
- begin
- HClip := GetClipboardData(cf_Bitmap);
- if HClip <> 0 then
- begin
- DC := GetDC(HWindow);
- MemDC1 := CreateCompatibleDC(DC);
- MemDC2 := CreateCompatibleDC(DC);
- GetObject(HClip, Sizeof(TBitmap), @BitmapRec);
- Width := BitmapRec.BMWidth;
- Height := BitmapRec.BMHeight;
- Bitmap := CreateCompatibleBitmap(DC, Width, Height);
- OldBits1 := SelectObject(MemDC1, HClip);
- OldBits2 := SelectObject(MemDC2, Bitmap);
- BitBlt(MemDC2, 0, 0, Width, Height, MemDC1, 0, 0, SrcCopy);
- SelectObject(MemDC1, OldBits1);
- SelectObject(MemDC2, OldBits2);
- DeleteDC(MemDC1);
- DeleteDC(MemDC2);
- ReleaseDC(HWindow, DC)
- end;
- CloseClipboard
- end;
- InvalidateRect(HWindow, nil, true)
- end;
-
- {- Clear window contents }
- procedure GetBitsWindow.CMClear(var Msg: TMessage);
- begin
- DestroyBitmap;
- InvalidateRect(HWindow, nil, true)
- end;
-
- {- Paint bitmap inside window }
- procedure GetBitsWindow.Paint(PaintDC: HDC;
- var PaintInfo: TPaintStruct);
- var
- MemDC: HDC;
- OldBitmap: HBitmap;
- begin
- TWindow.Paint(PaintDC, PaintInfo);
- if Bitmap <> 0 then
- begin
- MemDC := CreateCompatibleDC(PaintDC);
- OldBitmap := SelectObject(MemDC, Bitmap);
- BitBlt(PaintDC, 10, 10, Width, Height, MemDC, 0, 0, SRCCopy);
- SelectObject(MemDC, OldBitmap);
- DeleteDC(MemDC)
- end
- end;
-
- var
-
- GetBitsApp: GetBitsApplication;
-
- begin
- GetBitsApp.Init('GetBitsApp');
- GetBitsApp.Run;
- GetBitsApp.Done
- end.
-
-
- {--------------------------------------------------------------
- Copyright (c) 1991 by Tom Swan. All rights reserved.
- Revision 1.00 Date: 5/26/1991
- ---------------------------------------------------------------}
-